package com.sxy.config;

import com.sxy.controller.Interceptor.BSInterceptor;
import com.sxy.controller.common.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private BSInterceptor bsInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration ir1 = registry.addInterceptor(bsInterceptor);
        //设置拦截路径
        ir1.addPathPatterns("/**");
        //设置不被拦截的路径
        ir1.excludePathPatterns(
                "/employee/login",
                "/employee/logout",
                "/user/login",
                "/user/sendMsg",
                "/backend/**",
                "/front/**",
                "/**/*.js",
                "/**/*.html",
                "/**/*.css",
                "/**/*.ico"
        );
    }

    /**
     * 扩展mvc框架的消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将消息转换器追加到mvc框架的转换器集合中,设置index把消息转换器对象添加到集合中最前位置
        converters.add(0,messageConverter);

    }
}
